#include <func.h>

typedef struct 
{
    pthread_mutex_t mutex;
    pthread_cond_t cond;
}data_t;
void* thread_func(void* p)
{
    //子线程等待在对应的条件变量上
    printf("i am child,will wait\n");
    struct timespec abstime;
    abstime.tv_sec=time(NULL)+4;
    abstime.tv_nsec=0;
    data_t* d=(data_t*)p;
    pthread_mutex_lock(&d->mutex);
    int ret=pthread_cond_timedwait(&d->cond,&d->mutex,&abstime);
    pthread_mutex_unlock(&d->mutex);
    printf("i am child,wakeup start work,timewait ret=%d\n",ret);
    pthread_exit(NULL);
}
int main()
{
    data_t d;
    int ret ;
    ret=pthread_mutex_init(&d.mutex,NULL);
    THREAD_ERROR_CHECK(ret,"pthread_mutex_init")
    ret=pthread_cond_init(&d.cond,NULL); 
    THREAD_ERROR_CHECK(ret,"pthread_cond_init")
    //创建子线程
    pthread_t pthid;
    pthread_create(&pthid,NULL,thread_func,&d);
    sleep(5);
    //负责唤醒
    ret=pthread_cond_signal(&d.cond);
    THREAD_ERROR_CHECK(ret,"pthread_cond_signal")
    //join并回收子线程
    ret=pthread_join(pthid,NULL);
    THREAD_ERROR_CHECK(ret,"pthread_join")
    printf("i am main  %d\n",ETIMEDOUT);
    return 0;
}